package com.msb.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.msb.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 支付宝的配置文件
 */
//@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 商户appid 沙箱账号: wwwyxs9125@sandbox.com
    public static String APPID = "2021000121695206";
    // 私钥 pkcs8格式的
    public static String RSA_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDCCmrVGa/LHLKLr9NiR9fDqNG3NqKY7tRgWhtPbuHnAQUJqs14hrW2XcDq4kIIwzCZ+58uozBR5Gyg8sFgt8gnqap4pFUpgz/eJpF6W0olTRYl7mEVbnwrZUed7F03J56j6cFk0HPnThaUzO3JW59X/r7+Q8/llDCI6TPkz2HDNhxRqN2lLRa9WGEztdPc/nRpQiIJqeIuZkydMibMFQeFxKVKLeYll+1ktawDc1RkhRY3gz1FBo5VY27H6MFcZmfQpmUU1X1OOdF4UKwkWk67Zki8YAR/gaA+yQda4uz5WlVMjfawxJCofGVGdyBruVbA0IePBIuXcY3zUnQkH8SxAgMBAAECggEAUP7AMUuTkZjc/mnaNn/+lHF3eJ6ZqHSoub9i/vkt2psWWz6snc69Mu5XhLQFFFtFgkEB5RnBJa3Pmi86gQC1z7iGq+0Mj1N16Wj3+Lb6cC+JOzQR2jxQIF0aU9VB46/qDEmarFZGyxpRWrNf3lkcXkK7vq7XrOD6kWumebAHAtBsExHOUPQsx1DiFxV5R4Ocnoh9HQIqUInnH5wlKir2MFPfkX2BP50pC00IJfxLHFDHMPg9wHyy+geRrCCLkzzMTbs+gHeHiLwkIO01ioaktRXd6+wQbFxDDzYgsXacv9VZKItMG5Y37vpbh2U0vcZli+0E3ete3B3RHPUf0QPIAQKBgQD6gKOHu08ANF7N+Buc7BjOEftA3OIgVI9RohrNB3nlxaCno0WuF9lUxasLRxp62O1M53g8QdBHHSD6c300QnZXrl/F7ifmBEBpdOSIvgfFjmagAmCBsRx+zemf7kFwttzdA0f51SDJQlKL+FTBxs0+oZJmC/bFepQsDwsEJVrmUQKBgQDGTJFIQyhpmi2hYe/ubN1gP1jjS2gftL4EIzm+ZulctpN3JQtODa8+JJSvAbSC68uPWdsHEdzR4ZRvISeKEkK3zqf7KZ3Bz0YidDsnVSOj56/v+yY2ekcwrknAEWojvyhgdiaJ1rbfuAtH05x5Cyh6n0/w+NnsOUUFaGT24aaAYQKBgD5v99KqBjvn0cWG1OMdGeWrJ7P4GkiyqvJO9F0sQt7ZjnyaPpDxEtOFXWe3NktubF9hxl9GjDJdeU3ToV54PmUGaoq8HKfBYYingRrwOqGsJ7o0PXe1/INCsbuSOC9u9MgFvFfyHnTrmgWXRRDzi07UGNhknQKF1g7RRQ6ofOqxAoGBALnjLtG5utl7oUjGYT91J/TAjVfM6VfHe9SvHMNC/4Fc2VH4FOffZiZv7j078PZFifXgerTe5syMlTOztbf1tDtP6yJD+dGjtxA3Ovh9FELivofNPB4XdELLLvFwobnOyU/bkfbmo1mG0nRLh0Pk1NZWH386GDcb3HrcSMEx+H4hAoGBAMDPx1t41iGGqSvAifHImaYO+NMy4MZ3wXtmKC2RXsQxGjj/EoGgnO68fZ6Ic64ZrtsFwD0vMmPu+dCM2TYBZjNOnVuJoKiMo4Aznxw1Aa0tktljY0Y58+NzGXp/ZIIRLJrujk1bmBIOgIdIIH4O5fD5a3C/5W1nJ+7tCY6Mbeqz";
    // 服务器异步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url = "http://order.msb.com:8030/payed/notify";
    // 页面跳转同步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问 商户可以自定义同步跳转地址
    public static String return_url = "http://order.msb.com:8030/orderPay/returnUrl";
    // 请求网关地址
    public static String URL = "https://openapi.alipaydev.com/gateway.do";
    // 编码
    public static String CHARSET = "UTF-8";
    // 返回格式
    public static String FORMAT = "json";
    // 支付宝公钥
    public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5/Vwmif7G1VulYNTPFPYN09kSmjXiTAB9Dcnr1W+bQoj/IspjGYPU2uPUhdnSYu752YZJGQ7g/x3PzmbUyQiGqRPcFR66W7oKj3GzqfBdO9x+2JEakunjxXk3unjDUHZNNfMzZefiNmE6hzFjO8zVRrEZRnAaA8169oK4uGaLj2eyNr4XpFkx8hB7h5hm324CV6+2TNg/BW2aNUYjx2X8dCrjajgFzykzjaIJEOfPLmNmwjDDxMQen3td/9EXbkj3b6zoAldfTqVDOs2kdnXnCi+6k2VWsuiga8VX6YSv3xY+svnPsIy1cE8s0SWYuaTlM5iGqhmfj7cGCV67ikR/wIDAQAB";
    // 日志记录目录
    public static String log_path = "/log";
    // RSA2
    public static String SIGNTYPE = "RSA2";

    public String pay(PayVo payVo){
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        //调用RSA签名方式
        AlipayClient client = new DefaultAlipayClient(URL,
                APPID,
                RSA_PRIVATE_KEY,
                FORMAT,
                CHARSET,
                ALIPAY_PUBLIC_KEY,
                SIGNTYPE);
        AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();

        // 封装请求支付信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        model.setOutTradeNo(payVo.getOut_trader_no());
        model.setSubject(payVo.getSubject());
        model.setTotalAmount(payVo.getTotal_amount());
        model.setBody(payVo.getBody());
        model.setTimeoutExpress("5000");
        model.setProductCode("11111");
        alipay_request.setBizModel(model);
        // 设置异步通知地址
        alipay_request.setNotifyUrl(notify_url);
        // 设置同步地址
        alipay_request.setReturnUrl(return_url);

        // form表单生产
        String form = "";
        try {
            // 调用SDK生成表单
            form = client.pageExecute(alipay_request).getBody();
            return form;
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return  null;
    }
}
